package com.jingli.pile.util;

import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.hutool.crypto.symmetric.AES;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class PileSignUtils {
    private static Logger logger = LoggerFactory.getLogger(PileSignUtils.class);
    /**
     * aes 加密
     * @param params 待加密参数
     * @param dataSecret 密钥
     * @param dataSecretIV 密钥向量
     * @return 解密结果
     */
    public static String signParams(String params,String dataSecret,String dataSecretIV){
        AES aes = new AES("CBC","PKCS5Padding",dataSecret.getBytes(),dataSecretIV.getBytes());
        return aes.encryptBase64(params);
    }
    /**
     * aes 加密
     * @param params 待加密参数
     * @param dataSecret 密钥
     * @param dataSecretIV 密钥向量
     * @return 解密结果
     */
    public static String decryptParams(String params,String dataSecret,String dataSecretIV){
        AES aes = new AES("CBC","PKCS5Padding",dataSecret.getBytes(),dataSecretIV.getBytes());
        return aes.decryptStr(params);
    }

    public static String getSign(String data,String signSecret,String platformId){
        long timeStamp = new Date().getTime();
        int nextSequence = SequenceGenerator.getNextSequence();
        int len  = 64-signSecret.length();
        if (signSecret.length()<64){
            for (int i = 0; i < len; i++) {
               signSecret = signSecret + "0";
            }
        }
        logger.info(signSecret);
        byte[] key = signSecret.getBytes();
        HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
        String str =  mac.digestHex(data);
        return str;
    }
}
